#include "EpollServer.hpp"
#include <memory>

Response Calculate(const Request &req)
{
    //直接接入线程池
    // 走到这里，一定保证req是有具体数据的！
    // _result(result), _code(code)
    Response resp;
    switch (req._op)
    {
    case '+':
        resp._result = req._x + req._y;
        break;
    case '-':
        resp._result = req._x - req._y;
        break;
    case '*':
        resp._result = req._x * req._y;
        break;
    case '/':
        if (req._y == 0)
            resp._code = 1;
        else
            resp._result = req._x / req._y;
        break;
    case '%':
        if (req._y == 0)
            resp._code = 2;
        else
            resp._result = req._x % req._y;
        break;
    default:
        resp._code = 3;
        break;
    }

    return resp;
}

static void usage(char *argv)
{
    std::cout << "Usage:\n\t" << argv << std::endl;
}

int main(int argc,char*argv[])
{

    if (argc != 2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);
    std::unique_ptr<EpollServer> ssptr(new EpollServer(Calculate,port));
    ssptr->InitServer();
    ssptr->Dispatcher();

    return 0;
}